using System.Collections.Generic;
using Game.Const;
using Game.Core;
using Game.Main.Model;
using Game.Main.View;
using Game.Manager;
using UnityEngine;

namespace Game.Component
{
	public class MedicinePackerItem : BaseItem
	{
		private List<WaitPackItem> waitPackItems = new List<WaitPackItem>();

		public override void Init(ItemArchiveData data)
		{
			base.Init(data);
		}

		protected override void LateUpdate()
		{
			for (int num = waitPackItems.Count - 1; num >= 0; num--)
			{
				WaitPackItem waitPackItem = waitPackItems[num];
				waitPackItem.time += Time.deltaTime;
				if (!(waitPackItem.time > 0.6f))
				{
					continue;
				}
				if (waitPackItem.item.attachType != AttachType.Pack && waitPackItem.item != null)
				{
					BaseItem baseItem = Singleton<Scene>.Instance.CreateItem(waitPackItem.item.itemData.createIds[0], waitPackItem.item.transform.position, 0);
					if (baseItem != null)
					{
						ReleaseAttachItem(waitPackItem.item);
						baseItem.AttachItem(waitPackItem.item, DepthType.Back, AttachType.Pack, false);
						baseItem.DropDown(waitPackItem.item.transform.position, true);
						Singleton<AudioManager>.Instance.PlaySound("sound_explode");
						Singleton<EffectManager>.Instance.ShowEffect(PathConst.EFFECT + "efc_explode_1", baseItem.transform.position + new Vector3(0f, 0.1f, -0.0004f), 1f);
						Singleton<EffectManager>.Instance.ShowEffect(PathConst.EFFECT + "efc_blink_1", baseItem.transform.position + new Vector3(0f, 0.1f, -0.0004f), 3f);
						continue;
					}
				}
				waitPackItems.RemoveAt(num);
			}
		}

		public override bool AttachItem(BaseItem item, DepthType depthType, AttachType attachType, bool updatePosition)
		{
			if (item.itemData.type == 10 && item.itemData.createIds != null && item.itemData.createIds.Length > 0 && !IsContainsItem(item))
			{
				waitPackItems.Add(new WaitPackItem(item, 0f));
			}
			return base.AttachItem(item, depthType, attachType, updatePosition);
		}

		public override void ReleaseAttachItem(BaseItem item)
		{
			for (int i = 0; i < waitPackItems.Count; i++)
			{
				if (waitPackItems[i].item == item)
				{
					waitPackItems.RemoveAt(i);
					break;
				}
			}
			base.ReleaseAttachItem(item);
		}

		public bool IsContainsItem(BaseItem item)
		{
			for (int i = 0; i < waitPackItems.Count; i++)
			{
				if (waitPackItems[i].item == item)
				{
					return true;
				}
			}
			return false;
		}
	}
}
